<div class="deliman-wrapper">
<div class="section" id="sitemaplist">

	<div class="section-title">Quản lý nhân viên</div>
    
    <div class="section-content padding1">
    	
    	<form name="frm" id="frm" action="<?php echo $action?>" method="post" enctype="multipart/form-data">
        
        	<div class="button right">
            	<a class="button save" onclick="save()">Lưu</a>
                <a class="button cancel" href="?route=deliman/nhanvien">Bỏ qua</a>
     	        <input type="hidden" name="nhanvienid" value="<?php echo $item['nhanvienid']?>">   
     	        <input type="hidden" name="loainhanvienid" value="<?php echo $item['loainhanvienid']?>">
                <input type="hidden" name="masonhanvien" value="<?php echo $item['masonhanvien']?>">
            </div>
            <div class="clearer">^&nbsp;</div>
        	<div id="error" class="error" style="display:none"></div>
            
        	<div>
                <div class="left col2">
                    <p>
                        <label>Tên nhân viên:</label><br />
                        <input type="text" name="hoten" value="<?php echo $item['hoten']?>" class="text" size=60 />
                    </p>    
                    
                    <p>
                        <label>Giới tính:</label><br />
                        <input type="radio" name="gioitinh" value="0" <?php echo ($item['gioitinh']==0)?"checked":""?>/>Nam
                        <input type="radio" name="gioitinh" value="1" <?php echo ($item['gioitinh']==1)?"checked":""?> />Nữ
    
                    </p>    
                    
                    <p>
                        <label>Địa chỉ:</label><br />
                        <input type="text" name="diachi" value="<?php echo $item['diachi']?>" class="text" size=60 />
                    </p>
                    
                    <p>
                        <label>Email:</label><br />
                        <input type="text" name="email" value="<?php echo $item['email']?>" class="text" size=60 />
                    </p>    
                    
                    <p>
                        <label>Điện thoại:</label><br />
                        <input type="text" name="dienthoai" value="<?php echo $item['dienthoai']?>" class="text" size=60 />
                    </p>
                    <p>
                    	<label>Tạo tài khoản đăng nhập:</label>
                        <input type="radio" class="cotaikhoan" name="cotaikhoan" value="co" /> Có
                        <input type="radio" class="cotaikhoan" name="cotaikhoan" value="khong" checked="checked"/> Không
                    </p>
                    <div class="account">              
                        <p>
                            <label>Tên đăng nhập:</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <input type="text" class="text account" name="userid" value="<?php echo $item['userid']?>" <?php echo $item['userid']!=''?'readonly="readonly"':'' ?>/>
                        </p>
                        <?php if($item['userid']==""){ ?>
                        <p>
                            <label>Mật khẩu:</label>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <input type="password" class="text account" name="password" />
                        </p>
                        <p>
                            <label>Nhập lại mật khẩu:</label>
                            <input type="password" class="text account" name="confirmpassword" />
                        </p>
                        <?php } ?>
                    </div>
                </div>
                <div class="right col2">
                    <p>
                        <label>Tên loại nhân viên:</label><br />
                        <select id="loainhanvienid" name="loainhanvienid">
                                              
                        <?php 
                            foreach ($loainhanviens as $loainhanvien)
                            {
                        ?>
                            <option value="<?php echo $loainhanvien['loainhanvienid'] ?>">
                                <?php echo $loainhanvien['tenloainhanvien']?>
                            </option>
                        <?php
                            }
                        ?>
                        </select>
                    </p>
                    <div id="phutrachkhuvuc">
                        <p>
                            <label>Khu vực phụ trách</label>
                            <table>
                                <tbody id="listkhuvuc">
                                </tbody>
                            </table>
                            <input type="hidden" id="khuvucdel" name="khuvucdel"/>
                        </p>         
                        <p>
                            <label>Khu vực</label>
                            <div>
                                
                                <ul id="group0" class="filetree">
                                
                                </ul>    
                        
                            </div>
                        </p>
                    </div>
                </div>
                <div class="clearer">^&nbsp;</div>
            </div>
            
        </form>
        
    
    </div>
    
</div>
</div>

<script language="javascript">
//load nhóm nhân viên
$('#loainhanvienid').change(function(e) {
    if(this.value == 2 || this.value == 3)
	{
		$('#phutrachkhuvuc').show();
		loadTree(this.value);
	}
	else
	{
		$('#phutrachkhuvuc').hide();
	}
});
function loadTree(loainhanvienid)
{
	$('#group0').load("?route=deliman/nhanvien/getTreeSelectView&root=0&loainhanvienid="+loainhanvienid,
		function(){
			$("#group0").treeview();
			$('#listkhuvuc').html('');
			<?php foreach($khuvucphutrach as $val){ ?>
			khuvuc.add("<?php echo $val['id']?>","<?php echo $val['khuvucid']?>");
			<?php } ?>		
		});	
}

$(document).ready(function(){
	$('#loainhanvienid').val("<?php echo $item['loainhanvienid'] ?>");
	$('#loainhanvienid').change();
	
	
	$('.cotaikhoan').change(function(e) {
        if(this.value=='co')
		{
			$('.account').attr('disabled',false);	
			$('.account').show();
		}
		else
		{
			$('.account').attr('disabled','disabled');	
			$('.account').hide();
		}
		
    });
	$('.cotaikhoan').each(function(index, element) {
        if(this.value == "<?php echo $item['cotaikhoan']?>")
		{
			this.checked = true
		}
    });
	if("<?php echo $item['cotaikhoan']?>" == 'co')
	{
		$('#account').attr('disabled',false)
		$('.account').show();
	}
	else
	{
		$('.account').attr('disabled','disabled');
		$('.account').hide();
	}
});

function selectKhuVuc(khuvucid)
{
	khuvuc.add('',khuvucid);
}

function KhuVuc()
{
	this.index = 0;
	this.add = function(id,khuvucid)
	{
		var fkvtontai = false;
		$('.khuvuphutrach').each(function(index, element) {
			if(khuvucid == this.value)
			{
				alert("Bạn đã chọn khu vực này rồi");
				fkvtontai = true;	
			}
        });
		if(fkvtontai == false)
		{
			$.getJSON("?route=deliman/khuvuc/getKhuVuc&khuvucid="+khuvucid,
				function(data){
					var celltenkhuvuc='<td><input type="hidden" id="id-'+ khuvuc.index +'" name="id['+ khuvuc.index +']" value="'+ id +'"> <input type="hidden" id="khuvuphutrach-'+ khuvuc.index +'" name="khuvuphutrach['+ khuvuc.index +']" class="khuvuphutrach" value="'+ data.khuvuc.khuvucid +'">'+ data.khuvuc.tenkhuvuc +" "+ data.khuvuc.tenphuongxa + " " + data.khuvuc.tenquanhuyen+ " " + data.khuvuc.tentinhthanh    +'</td>';
					var cellbutton = '<td><a class="button-small delete" onclick="khuvuc.remove('+ khuvuc.index +')"></a></td>';
					var row = '<tr id="row'+ khuvuc.index +'">'+celltenkhuvuc+cellbutton+'</tr>';
					$('#listkhuvuc').append(row);
					khuvuc.index++;
					
				});
			
		}
		
	}
	
	this.remove = function(pos)
	{
		//var khuvucid = $('#khuvuphutrach-' + pos).val();
		$('#khuvucdel').val($('#khuvucdel').val() + $('#id-'+pos).val() + ",");
		$('#row'+pos).remove();
	}
}

var khuvuc = new KhuVuc();
<!--thực hiện chức năng save dữ liệu-->
function save()
{
	$.blockUI({ message: "<h1>Please wait...</h1>" }); 
	
	$.post("?route=deliman/nhanvien/save", $("#frm").serialize(),
		function(data){
			if(data == "true")
			{
				window.location = "?route=deliman/nhanvien";
			}
			else
			{
			
				$('#error').html(data).show('slow');
				$.unblockUI();
				
			}
			
		}
	);
}
</script>
